package com.itheima.dao.impl;

import com.itheima.dao.EmpDao;

import java.sql.*;
import java.util.List;

public class EmpDaoImpl implements EmpDao {
    Connection conn = null;
    Statement st = null;

    /**
     * @param num
     * @return
     * 3.员工表中存入100个员工
     */
    @Override
    public int addEmp(int num) {
        int j = 0;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db5", "root", "1234");
            st = conn.createStatement();

          int i =1;
 //          新增员工
            while (i <= num) {
                String empSql = "INSERT INTO t_emp VALUES (NULL,'jack" + i + "',now(),10000,1); ";
                i = i+1;
                j = st.executeUpdate(empSql);
            }
//
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (st != null) {
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return j;



    }

    /**
     * @return 4.查询所有员工及其部门的名称
     */
    @Override
    public List<List> findAllEmpAndDept() {
        int j = 0;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db5", "root", "1234");
            st = conn.createStatement();

            int i =1;
            //          查询所有员工

                String allEmpSql = "INSERT INTO t_emp VALUES (NULL,'jack" + i + "',now(),10000,1); ";
                i = i+1;
                j = st.executeUpdate(empSql);

//
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (st != null) {
                try {
                    st.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        return null;
    }
}

